package com.reporting.yb.module.system.service.reporting;

import com.reporting.yb.framework.common.pojo.PageResult;
import com.reporting.yb.framework.common.util.object.BeanUtils;
import com.reporting.yb.module.system.controller.admin.reporting.vo.ReportingPageReqVO;
import com.reporting.yb.module.system.controller.admin.reporting.vo.ReportingSaveReqVO;
import com.reporting.yb.module.system.dal.dataobject.reporting.ReportingDO;
import com.reporting.yb.module.system.dal.dataobject.reportingfile.ReportingFileDO;
import com.reporting.yb.module.system.dal.mysql.reporting.ReportingMapper;
import com.reporting.yb.module.system.dal.mysql.reportingfile.ReportingFileMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.reporting.yb.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.reporting.yb.module.system.enums.ErrorCodeConstants.REPORTING_NOT_EXISTS;

/**
 * 投诉信息 Service 实现类
 *
 * @author 123
 */
@Service
@Validated
public class ReportingServiceImpl implements ReportingService {

    @Resource
    private ReportingMapper reportingMapper;
    @Resource
    private ReportingFileMapper reportingFileMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createReporting(ReportingSaveReqVO createReqVO) {
        // 插入
        ReportingDO reporting = BeanUtils.toBean(createReqVO, ReportingDO.class);
        reportingMapper.insert(reporting);

        // 插入子表
        createReportingFileList(reporting.getId(), createReqVO.getReportingFiles());
        // 返回
        return reporting.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateReporting(ReportingSaveReqVO updateReqVO) {
        // 校验存在
        validateReportingExists(updateReqVO.getId());
        // 更新
        ReportingDO updateObj = BeanUtils.toBean(updateReqVO, ReportingDO.class);
        reportingMapper.updateById(updateObj);

        // 更新子表
        updateReportingFileList(updateReqVO.getId(), updateReqVO.getReportingFiles());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteReporting(String id) {
        // 校验存在
        validateReportingExists(id);
        // 删除
        reportingMapper.deleteById(id);

        // 删除子表
        deleteReportingFileByReportingId(id);
    }

    private void validateReportingExists(String id) {
        if (reportingMapper.selectById(id) == null) {
            throw exception(REPORTING_NOT_EXISTS);
        }
    }

    @Override
    public ReportingDO getReporting(String id) {
        return reportingMapper.selectById(id);
    }

    @Override
    public PageResult<ReportingDO> getReportingPage(ReportingPageReqVO pageReqVO) {
        return reportingMapper.selectPage(pageReqVO);
    }
    @Override
    public PageResult<ReportingDO> getListreporting(String category) {
        return reportingMapper.getListreporting(category);
    }

    // ==================== 子表（投诉文件） ====================

    @Override
    public List<ReportingFileDO> getReportingFileListByReportingId(String reportingId) {
        return reportingFileMapper.selectListByReportingId(reportingId);
    }

    private void createReportingFileList(String reportingId, List<ReportingFileDO> list) {
        list.forEach(o -> o.setReportingId(reportingId));
        reportingFileMapper.insertBatch(list);
    }

    private void updateReportingFileList(String reportingId, List<ReportingFileDO> list) {
        deleteReportingFileByReportingId(reportingId);
        list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createReportingFileList(reportingId, list);
    }

    private void deleteReportingFileByReportingId(String reportingId) {
        reportingFileMapper.deleteByReportingId(reportingId);
    }

}